import { postUserList, postPageListData, deletePageById } from '@/service/main/system/system'
import { defineStore } from 'pinia'
import type { ISystemState } from './type'
import { editPageData, newPageData } from '@/service/main/system/system'
import userMainStore from '../main'

const useSystemStore = defineStore('system', {
  state: (): ISystemState => ({
    usersList: [],
    usersTotalCount: 0,

    pageList: [],
    pageTotalCount: 0
  }),
  actions: {
    async postUserListAction(data: any) {
      const {
        data: { list, totalCount }
      } = await postUserList(data)
      this.usersList = list
      this.usersTotalCount = totalCount
    },
    /** 针对页面的数据: 增删改查 */
    async postPageListAction(pageName: string, queryInfo: any) {
      const pageListResult = await postPageListData(pageName, queryInfo)
      const { totalCount, list } = pageListResult.data

      this.pageList = list
      this.pageTotalCount = totalCount
    },
    async editPageDataAction(pageName: string, id: number, pageInfo: any) {
      await editPageData(pageName, id, pageInfo)
      // 获取更新后的数据
      this.postPageListAction(pageName, { offset: 0, size: 10 })
      // 获取完整的数据
      const mainStore = userMainStore()
      mainStore.fetchEntireDataAction()
    },
    async newPageDataAction(pageName: string, pageInfo: any) {
      const newResult = await newPageData(pageName, pageInfo)
      console.log(newResult, 'newResult')
      this.postPageListAction(pageName, { offset: 0, size: 10 })

      // 获取完整的数据
      const mainStore = userMainStore()
      mainStore.fetchEntireDataAction()
    },
    async deletePageByIdAction(pageName: string, id: number) {
      const deleteResult = await deletePageById(pageName, id)
      console.log(deleteResult)
      this.postPageListAction(pageName, { offset: 0, size: 10 })

      // 获取完整的数据
      const mainStore = userMainStore()
      mainStore.fetchEntireDataAction()
    }
  }
})

export default useSystemStore
